package com.ita.service.impl;

import javax.jms.JMSException;

import org.apache.log4j.Logger;

import com.ita.activemq.JMSSender;
import com.ita.activemq.ProducerManager;
import com.ita.dao.MerchantDao;
import com.ita.dao.impl.MerchantDaoImpl;
import com.ita.pojo.Merchant;
import com.ita.service.MerchantService;
import com.ita.util.JsonUtil;

public class MerchantServiceImpl implements MerchantService {

	private JMSSender sender;
	private Logger logger = Logger.getLogger(MerchantServiceImpl.class);
	private MerchantDao merchantDao;

	public MerchantServiceImpl() {
		try {
			sender = new ProducerManager();
			merchantDao = new MerchantDaoImpl();
		} catch (JMSException e) {
			logger.info(e.getMessage());
		}
	}

	@Override
	public int sendToMQ(Merchant m) {
		String json = JsonUtil.getJsonFromObject(m);
		sender.send(json);
		// generateImage(m);
		return 0;
	}

	@Override
	public int AddMerchant(Merchant m) {
		return merchantDao.addMerchant(m);
	}

	@Override
	public Merchant loadMerchant(String username) {
		return merchantDao.loadMerchant(username);
	}

	@Override
	public int updateMerchant(Merchant m) {
		// TODO Auto-generated method stub
		return merchantDao.updateMerchant(m);
	}

	@Override
	public void updateBlock(String id, Integer block) {
		merchantDao.updateBlock(id, block);

	}

	@Override
	public boolean checkPassword(String name, String password) {
		
		Merchant merchant = merchantDao.loadMerchant(name);
		
		return merchant.getPassword().equals(password);
	}
}
